@extends('common.base-master')

@section('html-head')
    <link rel="stylesheet" href="{{url('css/address.css')}}">
    <script src="{{url('js/address.js')}}"></script>
    <script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
@endsection

@section('html-body')
    @if(count($address_list) > 0)
        @foreach($address_list as $address)
    <div class="line-wrapper" id="addr_{{$address->id}}" data-id="{{$address->id}}">
        <div class="line-scroll-wrapper">
            <div class="line-normal-wrapper">
                <div class="line-normal-left-wrapper">
                    <div class="line-normal-avatar-wrapper @if($address->is_default == 1)bg-star_icon @endif"></div>
                    <div class="line-normal-info-wrapper">
                        <div class="line-normal-user-name">{{$address->realname}}，{{$address->phone}}</div>
                        <div class="line-normal-time">{{$address->province}}{{$address->city}}{{$address->area}}{{$address->info}}</div>
                    </div>
                </div>
                <div class="line-normal-icon-wrapper"></div>
            </div>
            <div class="line-btn-change"><button data-id="{{$address->id}}" class="change">修改</button></div>
            <div class="line-btn-delete"><button data-id="{{$address->id}}" class="del">删除</button></div>
        </div>
    </div>
        @endforeach
    @endif
    <div class="myadd-page">
    <a href="{{route('Home.User.changeAddress')}}" class="add-add">新增地址</a>
    </div>
    <script>
        $(document).ready(function () {
            var type = '{{$type}}';
            var gid  = '{{$gid}}';
            var num  = '{{$num}}';

            $('.del').click(function () {
                var id = $(this).data('id');
                $.ajax({
                    url:"{{route('Api.User.delUserAddress')}}",
                    type:'post',
                    dataType:'json',
                    data:{
                        id:id
                    },
                    success:function (obj) {
                        if(obj.level == 'success'){
                            $.toast(obj.message,function() {
                                $('#addr_'+id).hide('slow');
                            });
                        }else {
                            $.toast(obj.message,"forbidden");
                        }
                    },
                    error:function (obj) {
                        console.log(obj);
                    }
                });
            });

            $('.change').click(function () {
                var id = $(this).data('id');
                window.location.href="{{route('Home.User.changeAddress')}}?id="+id;
            });

            $('.line-wrapper').click(function () {
                var gid = $.cookie('gid');
                var num = $.cookie('num');
                var id = $(this).data('id');
                if(type == 'select'){
                    window.location.href="{{route('Home.Goods.goodsBuy')}}?id="+gid+"&address_id="+id;
                }else {
                    window.location.href="{{route('Home.Page.newOrder')}}?id="+gid+"&address_id="+id+"&num="+num;
                }
            });
        });
    </script>
@endsection